package com.eqdd.yiqidian.utils;

import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.bean.AreaBean;
import com.eqdd.yiqidian.http.HttpConfig;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.adapter.Call;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.convert.StringConvert;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import chihane.jdaddressselector.BottomDialog;
import chihane.jdaddressselector.DataProvider;
import chihane.jdaddressselector.ISelectAble;
import chihane.jdaddressselector.SelectedListener;
import chihane.jdaddressselector.Selector;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by lvzhihao on 17-4-25.
 */

public class MultiChooseUtil {

    private List<AreaBean> areaBeanSecond;
    private List<AreaBean> areaBeanThird;
    private List<AreaBean> areaBeanFour;
    private List<AreaBean> areaBeanFive;
    private Map<Integer, Integer> selectIds = new HashMap<>();
    OnMuiltiChooseSelectListener onMuiltiChooseSelectListener;

    public MultiChooseUtil() {
    }

    public interface OnMuiltiChooseSelectListener {
        void onMuiltiChooseSelectListener(Map<Integer, Integer> selectIds, ArrayList<ISelectAble> selectAbles);
    }

    public BottomDialog getAreaDialog(BaseActivity activity, int deep, List<AreaBean> areaBeanList, OnMuiltiChooseSelectListener onMuiltiChooseSelectListener) {
        this.onMuiltiChooseSelectListener = onMuiltiChooseSelectListener;
        this.areaBean = areaBeanList;
        Selector selector = new Selector(activity, deep);
        selector.setDataProvider(new DataProvider() {
            @Override
            public void provideData(int currentDeep, int preId, DataReceiver receiver) {
                //根据tab的深度和前一项选择的id，获取下一级菜单项
                selectIds.put(currentDeep, preId);
                System.out.println(currentDeep + "  " + preId);
                getDatas(currentDeep, preId, receiver);
            }
        });
        selector.setSelectedListener(new SelectedListener() {
            @Override
            public void onAddressSelected(ArrayList<ISelectAble> selectAbles) {
                onMuiltiChooseSelectListener.onMuiltiChooseSelectListener(selectIds, selectAbles);
            }
        });

        BottomDialog dialog = new BottomDialog(activity);
        dialog.init(activity, selector);
        return dialog;
    }

    List<AreaBean> areaBean = new ArrayList<>();

    private void getDatas(int currentDeep, int preId, DataProvider.DataReceiver receiver) {

        ArrayList<ISelectAble> selectAbles = null;
        switch (currentDeep) {
            case 0:
                selectAbles = getAreaData(areaBean);
                receiver.send(selectAbles);
                break;
            case 1:
                areaBeanSecond = areaBean.get(preId).getSub();
                selectAbles = getAreaData(areaBeanSecond);
                receiver.send(selectAbles);
                break;
            case 2:
                areaBeanThird = areaBeanSecond.get(preId).getSub();
                selectAbles = getAreaData(areaBeanThird);
                receiver.send(selectAbles);
                break;
            case 3:
                Observable.just(1)
                        .lift(new Observable.Operator<ArrayList<ISelectAble>, Integer>() {
                            @Override
                            public Subscriber<? super Integer> call(Subscriber<? super ArrayList<ISelectAble>> subscriber) {
                                return new Subscriber<Integer>() {
                                    @Override
                                    public void onCompleted() {
                                        subscriber.onCompleted();
                                    }

                                    @Override
                                    public void onError(Throwable e) {
                                        subscriber.onError(e);
                                    }

                                    @Override
                                    public void onNext(Integer integer) {
                                        try {
                                            String url = HttpConfig.BASE_URL + "areas/" + areaBeanThird.get(preId).getCode();
                                            Call<String> call = OkGo.<String>get(url)
                                                    .cacheMode(CacheMode.IF_NONE_CACHE_REQUEST)
                                                    .cacheTime(1000 * 60 * 60 * 24 * 7)
                                                    .converter(new StringConvert())
                                                    .adapt();
                                            Response<String> response = null;
                                            response = call.execute();
                                            areaBeanFour = new Gson().fromJson(response.body(), new TypeToken<List<AreaBean>>() {
                                            }.getType());
                                            subscriber.onNext(getAreaData(areaBeanFour));

                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }
                                    }
                                };
                            }
                        })
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe((i) ->
                                receiver.send(i));
                break;
            case 4:
                areaBeanFive = areaBeanFour.get(preId).getSub();
                selectAbles = getAreaData(areaBeanFive);
                receiver.send(selectAbles);
                break;
        }
    }

    private static ArrayList<ISelectAble> getAreaData(List<AreaBean> areaBeans) {
        ArrayList<ISelectAble> data = new ArrayList<>();

        for (int j = 0; j < areaBeans.size(); j++) {
            final int finalJ = j;
            data.add(new ISelectAble() {
                @Override
                public String getName() {
                    return areaBeans.get(finalJ).getName();
                }

                @Override
                public int getId() {
                    return finalJ;
                }

                @Override
                public Object getArg() {
                    return this;
                }
            });
        }
        return data;
    }
}
